package me.wangqu.weiyouqu.web.rest;

import me.wangqu.weiyouqu.web.rest.business.model.ListModel;
import me.wangqu.weiyouqu.web.rest.business.service.ArticleService;
import org.hibernate.loader.plan.spi.Return;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by joln on 2016/7/11.
 */
@RestController
@RequestMapping("/api")
public class ArticleResource {
    @Autowired
    private ArticleService articleService;

    private List<ListModel> listModels;

    /**
     * 获取文章列表
     *
     * @param code
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/articles",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<ListModel>> getCodeArticle(Integer code, Integer type, Pageable pageable) {
        if (type == 1) {
            listModels = new ArrayList<>();
            if (pageable.getPageNumber() == 0) {
                ListModel article = articleService.getCodeArticle(code, type, pageable);
                listModels.add(article);
                ListModel head = articleService.getCodeArticleHead(code, type);
                listModels.add(head);
            } else if (pageable.getPageNumber() > 0) {
                ListModel article = articleService.getCodeArticle(code, type, pageable);
                listModels.add(article);
            }
            return new ResponseEntity<>(listModels, HttpStatus.OK);
        }

        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }
}
